import Vue from 'vue'
import Router from 'vue-router'

const arch = () => import(/* webpackChunkName: "group-nav" */ '@/pages/archive')

const singleblog = () =>
    import(/* webpackChunkName: "group-blog" */ '@/pages/blog/blog.vue')

const Index = () =>
    import(/* webpackChunkName: "group-nav" */ '@/pages/bloglist/bloglist.vue')

const diary = () => import(/* webpackChunkName: "group-nav" */ '@/pages/diary')

// const clockIn = () =>
//     import( /* webpackChunkName: "group-nav" */ '@/pages/clockIn')

const project = () =>
    import(/* webpackChunkName: "group-nav" */ '@/pages/project')

const choice = () =>
    import(/* webpackChunkName: "group-nav" */ '@/pages/choice')

Vue.use(Router) //vue全局使用Router

//导出顶部导航栏 -- 基本路由
export const menuRoutes = [
    {
        path: '/index',
        name: 'index',
        component: Index,
        meta: {
            title: '首页',
            icon: 'home4',
        },
    },
    {
        path: '/arch',
        name: 'arch',
        component: arch,
        meta: {
            title: '归档',
            icon: 'archive',
        },
    },
    // {
    //     path: '/clockIn',
    //     name: 'clockIn',
    //     component: test,
    //     meta: {
    //         title: '测试',
    //         icon: 'daka-11',
    //     },
    // },

    {
        path: '/project',
        name: 'project',
        component: project,
        meta: {
            title: '项目',
            icon: 'project-fill',
        },
    },
    {
        path: '/diary',
        name: 'diary',
        component: diary,
        meta: {
            title: '日记',
            icon: 'book-fill',
        },
    },
]

//导出不需要右边介绍栏的页面
export const layoutUnneedRight = ['blog', 'project']

//导出路由器--动态匹配路由
const router = new Router({
    mode: 'history',
    routes: [
        {
            path: '/',
            redirect: '/index',
        },
        {
            props: true,
            path: '/archive/:id',
            name: 'blog',
            component: singleblog,
            meta: {
                title: '博文',
                icon: 'book-fill',
            },
        },
        {
            path: '/choice',
            name: 'choice',
            component: choice,
            meta: {
                title: '选择',
                icon: 'daka-11',
            },
        },
        ...menuRoutes,
    ],
})

const DEFAULT_TITLE = 'Huos  |  '

router.beforeEach((to, from, next) => {
    document.title = to.meta.title
        ? `${DEFAULT_TITLE}${to.meta.title}`
        : DEFAULT_TITLE
    next()
})

//路由跳转后，页面回到顶部
router.afterEach(() => {
    document.body.scrollTop = 0
    document.documentElement.scrollTop = 0
})
export default router
